package com.jvyou.app.ds.checker;

import com.jvyou.app.ds.domain.entity.Lesson;

import java.time.LocalDateTime;

/**
 * 课程状态判断工具类
 */
public class LessonStatusChecker {

    // 状态常量
    public static final String NORMAL = "normal";
    public static final String OVERDUE = "overdue";
    public static final String FORBID = "forbid";
    public static final String FULL = "full";
    public static final String STARTED = "start";

    /**
     * 判断课程状态（优先级从高到低）
     *
     * @param lesson 课程对象
     * @return 状态常量值
     */
    public static String checkStatus(Lesson lesson) {
        LocalDateTime now = LocalDateTime.now();
        // 1. 检查是否已过期
        if (isBefore(lesson.getEndTime(), now)) {
            return OVERDUE;
        }
        // 2. 检查是否已开始
        if (isBefore(lesson.getStartTime(), now) && isBefore(now, lesson.getEndTime())) {
            return STARTED;
        }
        // 3. 检查是否禁止预约
        if ("3".equals(lesson.getStatus())) {
            return FORBID;
        }
        // 4. 检查是否已满员
        if ("2".equals(lesson.getStatus()) || lesson.getCurrentStudents() >= lesson.getMaxStudents()) {
            return FULL;
        }
        // 5. 默认正常状态
        return NORMAL;
    }

    /**
     * 判断时间是否在某个时间之前
     *
     * @param time      待比较时间
     * @param compareTo 比较基准时间
     * @return 是否在之前
     */
    private static boolean isBefore(LocalDateTime time, LocalDateTime compareTo) {
        return time != null && compareTo != null && time.isBefore(compareTo);
    }

    public static String getErrorMessage(String status, String message) {
        switch (status) {
            case OVERDUE:
                return "课程已过期," + message;
            case FORBID:
                return "课程禁止预约," + message;
            case FULL:
                return "课程已满员," + message;
            case STARTED:
                return "课程已开始," + message;
            default:
                return "未知课程状态";
        }
    }
}